/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lczyfy.idcp.modules.crawl.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lczyfy.idcp.modules.crawl.entity.CrawlRelation;
import com.lczyfy.idcp.modules.crawl.service.CrawlRelationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lczyfy.idcp.common.config.Global;
import com.lczyfy.idcp.common.persistence.Page;
import com.lczyfy.idcp.common.web.BaseController;
import com.lczyfy.idcp.common.utils.StringUtils;
import com.lczyfy.idcp.modules.crawl.entity.CrawlRelation;
import com.lczyfy.idcp.modules.crawl.service.CrawlRelationService;

/**
 * 爬虫联系Controller
 * @author cw
 * @version 2018-07-20
 */
@Controller
@RequestMapping(value = "${adminPath}/crawl/crawlRelation")
public class CrawlRelationController extends BaseController {

	@Autowired
	private CrawlRelationService crawlRelationService;
	
	@ModelAttribute
	public CrawlRelation get(@RequestParam(required=false) String id) {
		CrawlRelation entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = crawlRelationService.get(id);
		}
		if (entity == null){
			entity = new CrawlRelation();
		}
		return entity;
	}
	
	@RequiresPermissions("crawl:crawlRelation:view")
	@RequestMapping(value = {"list", ""})
	public String list(CrawlRelation crawlRelation, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<CrawlRelation> page = crawlRelationService.findPage(new Page<CrawlRelation>(request, response), crawlRelation); 
		model.addAttribute("page", page);
		return "modules/crawl/crawlRelationList";
	}

	@RequiresPermissions("crawl:crawlRelation:view")
	@RequestMapping(value = "form")
	public String form(CrawlRelation crawlRelation, Model model) {
		model.addAttribute("crawlRelation", crawlRelation);
		return "modules/crawl/crawlRelationForm";
	}

	@RequiresPermissions("crawl:crawlRelation:edit")
	@RequestMapping(value = "save")
	public String save(CrawlRelation crawlRelation, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, crawlRelation)){
			return form(crawlRelation, model);
		}
		crawlRelationService.save(crawlRelation);
		addMessage(redirectAttributes, "保存爬虫联系成功");
		return "redirect:"+Global.getAdminPath()+"/crawl/crawlRelation/?repage";
	}
	
	@RequiresPermissions("crawl:crawlRelation:edit")
	@RequestMapping(value = "delete")
	public String delete(CrawlRelation crawlRelation, RedirectAttributes redirectAttributes) {
		crawlRelationService.delete(crawlRelation);
		addMessage(redirectAttributes, "删除爬虫联系成功");
		return "redirect:"+Global.getAdminPath()+"/crawl/crawlRelation/?repage";
	}

}